package com.slwish.xty.mvp.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.slwish.common.mvp.presenter.BasePresenter;
import com.slwish.common.utils.LogUtils;
import com.slwish.xty.R;
import com.slwish.xty.config.UrlConfig;
import com.slwish.xty.mvp.contract.SubscribeInfoContract;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.ProgressDialogCallBack;
import com.zhouyou.http.exception.ApiException;


/**
 * 模块说明：预约Presenter
 *
 * @author 陈浩
 * @date 2017/12/1
 */
public class SubscribeInfoPresenter extends BasePresenter<SubscribeInfoContract.View> implements SubscribeInfoContract.Presenter {
    public SubscribeInfoPresenter(SubscribeInfoContract.View view) {
        super(view);
    }


    @Override
    public void cancelSubscribe(Context mContext,int orderId) {
        EasyHttp.post(UrlConfig.CANCEL_SUBSCRIBE_URL)
                .params("orderId", orderId + "")
                .execute(new ProgressDialogCallBack<String>(showProgressDialog(mContext)) {
                    @Override
                    public void onError(ApiException e) {
                        setError(R.string.toast_yy_cancel_exception_text);
                    }

                    @Override
                    public void onSuccess(String json) {
                        LogUtils.i("xx", "json--->" + json);
                        try {
                            JSONObject jsonObject = JSON.parseObject(json);
                            String resultCode = (String) jsonObject.get("resultCode");
                            String resultMessage = (String) jsonObject.get("resultMessage");
//                            String data = (String) jsonObject.getString("data");
                            //return
                            setError(resultMessage);
                            if (!TextUtils.equals(resultCode, "0"))
//                                setError(resultMessage);
                                return;

//                            List<SubscribeModel> models = JSON.parseArray(data, SubscribeModel.class);
                            SubscribeInfoContract.View mview = getView();
                            if (mview != null) {
                                mview.cancelSuccess("");
                            }
                        } catch (Exception e) {
                            setError(R.string.toast_yy_cancel_exception_text);
                        }
                    }
                });
    }
}
